package com.tdogg.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.tdogg.entity.Funs;
import com.tdogg.entity.GoodsType;
import com.tdogg.entity.Role;
import com.tdogg.service.IPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    IPermissionService permissionService;

    @RequestMapping("findRolesBySplitPage")
    @ResponseBody
    public JSONObject findTypeBySplitPage(Integer page, Integer limit, String keyword) throws UnsupportedEncodingException {
        if(StringUtils.isNoneBlank(keyword)) {
            keyword = URLDecoder.decode(URLEncoder.encode(keyword,"iso8859-1"),"utf-8");
        }
        PageInfo<Role> info = permissionService.queryRolesBySplitePage(page,limit,keyword);
        JSONObject obj=new JSONObject();
        obj.put("code", 0);
        obj.put("msg", "");
        obj.put("count", info.getTotal());
        obj.put("data", info.getList());
        return obj;
    }
    
    @RequestMapping("findAllFuns")
    @ResponseBody
    public JSONObject findAllFuns(){
        List<Funs> funs = permissionService.queryAllFuns();
        JSONObject obj=new JSONObject();
        obj.put("data", funs);
        return obj;
    }
    @RequestMapping("addRole")
    @ResponseBody
    public JSONObject addRole(String roleName, Integer[] funId){
        String s = permissionService.addRoleWithPermission(roleName, funId);
        JSONObject obj=new JSONObject();
        if(s != null && s.equals("success")){
            obj.put("code", 0);
            obj.put("msg", "添加角色成功");
        }else{
            obj.put("code", 1);
            obj.put("msg", s);
        }
        return obj;
    }

    @RequestMapping("updateRole")
    @ResponseBody
    public JSONObject addRole(Role role, Integer[] funId){
        String s = permissionService.updateRolePermission(role, funId);
        JSONObject obj=new JSONObject();
        if(s != null && s.equals("success")){
            obj.put("code", 0);
            obj.put("msg", "编辑角色成功");
        }else{
            obj.put("code", 1);
            obj.put("msg", s);
        }
        return obj;
    }

    @RequestMapping("deleteRole")
    @ResponseBody
    public JSONObject deleteRole(Integer roleId){
        String s = permissionService.deleteRole(roleId);
        JSONObject obj=new JSONObject();
        if(s != null && s.equals("success")){
            obj.put("code", 0);
            obj.put("msg", "删除角色成功");
        }else{
            obj.put("code", 1);
            obj.put("msg", s);
        }
        return obj;
    }
    @RequestMapping("allRole")
    @ResponseBody
    public JSONObject findAllRole(){
        List<Role> list = permissionService.findAllRole();
        JSONObject obj=new JSONObject();
        obj.put("code", 0);
        obj.put("msg", "成功");
        obj.put("data", list);
        return obj;
    }

}
